package org.csu.gp_store.bo;

import lombok.Data;
import org.csu.gp_store.entity.Company;
import org.csu.gp_store.entity.Item;
import org.csu.gp_store.entity.Product;


import java.math.BigDecimal;
import java.util.List;

@Data
public class ProductBO {

    private String productID;
    private String productName;
    private String description;
    private String categoryID;
    private String companyID;
    private String companyName;
    private String companyDescription;
    private String companyEmail;
    private String companyAddress;
    private String image;
    private int status;
    private BigDecimal minPrice;
    private BigDecimal maxPrice;
    private List<Item> itemList;

    public ProductBO() {

    }

    public static ProductBO toProductBO(Company company, Product product, List<Item> itemList) {
        ProductBO productBO = new ProductBO();
        try {
            productBO.productID = String.valueOf(product.getProductID());
            productBO.productName = product.getProductName();
            productBO.companyID = String.valueOf(product.getCompanyID());
            productBO.description = product.getDescription();
            productBO.categoryID=String.valueOf(product.getCategoryID());
            productBO.companyName = company.getCompanyName();
            productBO.companyDescription = company.getDescription();
            productBO.companyEmail = company.getEmail();
            productBO.status=product.getStatus();
            productBO.companyAddress = company.getAddress();
            productBO.image = itemList.get(0).getImage();
            productBO.minPrice = BigDecimal.valueOf(999999);
            productBO.maxPrice = BigDecimal.valueOf(0);
            for (Item item : itemList) {
                if (item.getPrice().compareTo(productBO.minPrice) < 0) {
                    productBO.minPrice = item.getPrice();
                }
                if (item.getPrice().compareTo(productBO.getMaxPrice()) > 0) {
                    productBO.maxPrice = item.getPrice();
                }
            }
            productBO.itemList=itemList;


        } catch (Exception e) {
            e.printStackTrace();
        }
        return productBO;
    }
    public  static Product productBOToProduct(ProductBO productBO){
        Product product =new Product();
        product.setProductID(Long.valueOf(productBO.getProductID()));
        product.setProductName(productBO.getProductName());
        product.setCategoryID(Long.valueOf(productBO.getCategoryID()));
        product.setCompanyID(Long.valueOf(productBO.getCompanyID()));
        product.setDescription(productBO.getDescription());
        product.setStatus(productBO.getStatus());
        return product;
    }
}
